Here are the another set of examples using for loop to understand concept and usage of For Loop.
Example : Generating a Series
Generating fibonacci series example in java
public class Main{
public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}
Output
0 1 1 2 3 5 8 13 21 34
This for loop generates a Fibonacci series of length 10.
Example : Displaying ASCII Characters
display ASCII characters in java
public class Main{
public static void main(String[] args) {
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c + " ");
}
}
}
Output
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
The loop iterates through uppercase English letters and prints them.
Example : Multiplication Table
multiplication table in java
public class Main{
public static void main(String[] args) {
int num = 7;
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
This for loop generates the multiplication table for the number 7.
Reversing an Array
Array reverse using for loop
public class Main{
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = numbers.length - 1; i >= 0; i--) {
System.out.print(numbers[i] + " ");
}
}
}
Output
5 4 3 2 1
Here, the loop iterates through an array in reverse order and prints the elements.
Prime Number Checker
find a Prime number using java
public class Main{
public static void main(String[] args) {
int num = 17;
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(num + " is prime: " + isPrime);
}
}
Output
17 is prime: true
This for loop checks if a number is prime by testing its divisibility.
Displaying a Pattern
pattern printing using for loop
public class Main{
public static void main(String[] args) {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
1
1 2
1 2 3
1 2 3 4
This loop prints a pattern of numbers in a triangular shape.
Computing Exponential Values
Computing exponential Values example in java
public class Main{
public static void main(String[] args) {
int base = 2;
int exponent = 5;
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
System.out.println(base + " raised to the power of " + exponent + " is: " + result);
}
}
Output
2 raised to the power of 5 is: 32
This loop calculates the result of raising a number to an exponent.
Printing Odd Numbers Backward
Backward odd number
public class Main{
public static void main(String[] args) {
for (int i = 99; i >= 1; i -= 2) {
System.out.print(i + " ");
}
}
}
Output
99 97 95 93 91 89 87 85 83 81 79 77 75 73 71 69 67 65 63 61 59 57 55 53 51 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1
This for loop prints odd numbers in reverse order, from 99 down to 1.
These examples demonstrate the versatility of for loops in java for a wide range of tasks, from generating sequences and patterns to performing mathematical calculations and checks.